#
#	El manager v.0.01 - Derivart (http://www.derivart.info)
#	@Coded: Mar Cane / Martin Nadal (2009)
#
import os
from mutagen import mutagen

class readAndWriteID3():

    def __init__(self):
        version = "0.1"

# Method not used but good as reference
    def loadAllMetadata(self, filename):
        short_tags = full_tags = mutagen.File(filename)
        if isinstance(full_tags, mutagen.mp3.MP3):
            short_tags = mutagen.mp3.MP3(filename, ID3 = mutagen.easyid3.EasyID3)
        album = short_tags.get('album', ['No Album'])[0]
        artist = short_tags.get('artist', ['No Artist'])[0]
        duration = "%u:%.2d" % (full_tags.info.length / 60, full_tags.info.length % 60)
        length = full_tags.info.length
        title = short_tags.get('title', ['No Title'])[0]
        size = os.stat(filename).st_size
        copyright = short_tags.get('copyright', ['No Copyright'])[0]
# load metadata
    def loadMetadata(self, filename):
        title=""
        artist=""
        try:
            short_tags = full_tags = mutagen.File(filename)

            if isinstance(full_tags, mutagen.mp3.MP3):
                short_tags = mutagen.mp3.MP3(filename, ID3 = mutagen.easyid3.EasyID3)
            artist = short_tags.get('artist', ['No Artist'])[0]
            title = short_tags.get('title', ['No Title'])[0]
        except:
            pass
        return {"title":title,"artist":artist}
# save metadata
    def saveMetadata(self, filename, title, artist):
        short_tags = full_tags = mutagen.File(filename)
        if isinstance(full_tags, mutagen.mp3.MP3):
            short_tags = mutagen.mp3.MP3(filename, ID3 = mutagen.easyid3.EasyID3)
        short_tags['title'] = title
        short_tags['artist'] = artist
        short_tags.save()
# save Copyright of sound
    def saveCopyright(self, filename, copyright):
        short_tags = full_tags = mutagen.File(filename)
        if isinstance(full_tags, mutagen.mp3.MP3):
            short_tags = mutagen.mp3.MP3(filename, ID3 = mutagen.easyid3.EasyID3)
        short_tags['copyright'] = copyright
        short_tags.save()

#*******************************************************************************************
#r = readAndWriteID3()
#r.saveMetadata("musicTest/empty.ogg", "tomas","soy pollo")
#print r.loadMetadata('/Users/mcanet/Desktop/becas /2008/AVAM/spreadplayer_audio/spreadplayer_2.mp3')['artist']
#print r.loadMetadata("musicTest/vida.mp3")['artist']
#print r.loadMetadata("musicTest/vida.mp3")['title']